﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Diagnostics;
using Common.Tools.Encrypt;
using System.Text;

namespace UnitTestCommon.Tools
{
    [TestClass]
    public class TestEncrypt
    {

        [TestMethod]
        public void TestMD5()
        {
            string str = "lskdjfasldkflaksdflksadhflkasjdfhlkajsdhfauhfajhbvuerflaemrf;lasjfas;dfmasdfjkndl,v'kfdsdf";
            string md5StrA = MD5.Encrypt(str);
            string md5StrB = MD5.Encrypt(str, MD5Length.Sixteen);
            string md5StrC = MD5.Encrypt(str, MD5Length.ThirtyTwo);
            Assert.AreNotEqual(md5StrA, md5StrB);


            md5StrC = MD5.Encrypt("", MD5Length.ThirtyTwo);
            Assert.AreEqual("", md5StrC);
        }
        [TestMethod]
        public void TestDES()
        {
            string str = "lskdjfasldkflaksdflksadhflkasjdfhlkajsdhfauhfajhbvuerflaemr";
            string strEn = DES.Encrypt(str);
            string strDe = DES.Decrypt(strEn);
            Assert.AreNotEqual(str, strEn);
            Assert.AreEqual(str, strDe);

            strEn = DES.Encrypt(str, "ASDFGHJKL:");
            strDe = DES.Decrypt(strEn, "ASDFGHJKL:");
            Assert.AreEqual(str, strDe);
        }
        [TestMethod]
        public void TestRSA()
        {
            var kv = RSA.GetKeyPair();
            Assert.IsFalse(string.IsNullOrWhiteSpace(kv.Key));
            Assert.IsFalse(string.IsNullOrWhiteSpace(kv.Value));
            Assert.AreNotEqual(kv.Key, kv.Value);

            string str = "lskdjfasldkf";
            string strEn = RSA.Encrypt(str, kv.Key);
            string strDe = RSA.Decrypt(strEn, kv.Value);
            Assert.AreNotEqual(str, strEn);
            Assert.AreEqual(str, strDe);

            Stopwatch sw = new Stopwatch();
            sw.Restart();
            str = "1";
            for (int i = 0; i < 1000; i++)
            {
                str += Common.Extend.ExRandom.ExGenerateRandom(new Random(Guid.NewGuid().GetHashCode()), 1);
                strEn = RSA.Encrypt(str, kv.Key);
                strDe = RSA.Decrypt(strEn, kv.Value);
                Assert.AreNotEqual(str, strEn);
                Assert.AreEqual(str, strDe);
            }
            sw.Stop();

            str = "lskdjfasldkflaksdflks：“|{！@#￥……adhflkasjdfhlkajsd%%%hfauhfajhbvue#$^%&^*(%^&(&**#%$^#$%kjhsadlkfjhasf54dfg2sd4g13dx541g32xdf541gh3s5er1g3s2df13sd1gb3dx5frg16a5r4g/**-+^%&&*sdfgsdf54132f541vb2x41b54dfgv12dfvd&$%:|{}:?>rflaemr";//超长
            strEn = RSA.Encrypt(str, kv.Key);
            strDe = RSA.Decrypt(strEn, kv.Value);
            Assert.AreNotEqual(str, strEn);
            Assert.AreEqual(str, strDe);

            str = "";
            strEn = RSA.Encrypt(str, kv.Key);
            strDe = RSA.Decrypt(strEn, kv.Value);
            Assert.AreNotEqual(str, strEn);
            Assert.AreEqual(str, strDe);

            string publicKey = "";
            string privateKey = "";
            str = "!@#%$@#^%#$%^sdfga3df2sdf35v3d5f12zds1f2sd";
            strEn = RSA.Encrypt(str, out publicKey, out privateKey);
            Assert.IsFalse(string.IsNullOrWhiteSpace(publicKey));
            Assert.IsFalse(string.IsNullOrWhiteSpace(privateKey));
            Assert.AreNotEqual(publicKey, privateKey);
            Assert.AreNotEqual(str, strEn);
            strDe = RSA.Decrypt(strEn, privateKey);
            Assert.AreEqual(str, strDe);
        }
        [TestMethod]
        public void TestGetSimpleBytes()
        {
            string path = @"E:\Music\DJ\disco 我是真的不愿放开你的手 dj 庭锋.mp3";
            System.IO.FileInfo file = new System.IO.FileInfo(path);
            if (file.Exists)
            {
                int length = 1024 * 8;
                Stopwatch sw = new Stopwatch();
                sw.Restart();
                for (int i = 0; i < 100; i++)
                {
                    byte[] buffer = GetSimpleBytes(file.OpenRead(), length);
                }
                sw.Stop();
                Console.WriteLine(length+"简化耗时：" + sw.ElapsedMilliseconds);
                length = length * 2;

                sw.Restart();
                for (int i = 0; i < 100; i++)
                {
                    byte[] buffer = GetSimpleBytes(file.OpenRead(), length);
                }
                sw.Stop();
                Console.WriteLine(length + "简化耗时：" + sw.ElapsedMilliseconds);
                length = length * 2;

                sw.Restart();
                for (int i = 0; i < 100; i++)
                {
                    byte[] buffer = GetSimpleBytes(file.OpenRead(), length);
                }
                sw.Stop();
                Console.WriteLine(length + "简化耗时：" + sw.ElapsedMilliseconds);
                length = length * 2;

                sw.Restart();
                for (int i = 0; i < 100; i++)
                {
                    byte[] buffer = GetSimpleBytes(file.OpenRead(), length);
                }
                sw.Stop();
                Console.WriteLine(length + "简化耗时：" + sw.ElapsedMilliseconds);
                length = length * 2;

                sw.Restart();
                for (int i = 0; i < 100; i++)
                {
                    byte[] buffer = GetSimpleBytes(file.OpenRead(), length);
                }
                sw.Stop();
                Console.WriteLine(length + "简化耗时：" + sw.ElapsedMilliseconds);
                length = length * 2;

                sw.Restart();
                for (int i = 0; i < 100; i++)
                {
                    byte[] buffer = GetSimpleBytes(file.OpenRead(), length);
                }
                sw.Stop();
                Console.WriteLine(length + "简化耗时：" + sw.ElapsedMilliseconds);
                length = length * 2;

                sw.Restart();
                for (int i = 0; i < 100; i++)
                {
                    byte[] buffer = GetBytes(file.OpenRead());
                }
                sw.Stop();
                Console.WriteLine("耗时：" + sw.ElapsedMilliseconds);
            }
        }
        private static byte[] GetSimpleBytes(System.IO.Stream stream, int length)
        {
            byte[] array = new byte[length];
            int num = stream.Read(array, 0, length);
            if (num < length)
            {
                return array;
            }

            stream.Position = stream.Length - length;
            byte[] arrayLength = Encoding.Default.GetBytes(stream.Length.ToString());
            byte[] arrayNew = new byte[length * 2 + arrayLength.Length];
            Array.Copy(array, 0, arrayNew, 0, length);
            stream.Read(arrayNew, length, length);
            Array.Copy(arrayLength, 0, arrayNew, length * 2, arrayLength.Length);
            return arrayNew;
        }
        public static byte[] GetBytes(System.IO.Stream stream)
        {
            byte[] array = new byte[stream.Length + 4096 * 2];
            int num;
            int i = 0;
            do
            {
                num = stream.Read(array, i * 4096, 4096);
                i++;
            }
            while (num > 0);

            return array;
        }
    }
}
